package com.example.huazong.myapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText edt_Name, edt_Password;
    Button btn_dl, btn_zc;
    Intent intent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edt_Name = (EditText) findViewById(R.id.edt_Name);
        edt_Password = (EditText) findViewById(R.id.edt_Password);
        btn_dl = (Button) findViewById(R.id.btn_dl);
        btn_zc = (Button) findViewById(R.id.btn_zc);


        edt_Password.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置为密码输入框
        edt_Password.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});//最大输入长度6位
        String name = getIntent().getStringExtra("用户名");
        String password = getIntent().getStringExtra("密码");
        edt_Name.setText(name);
        edt_Password.setText(password);

        //创建对话框对象
        final AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);

        //设置对话框的标题、图标、提示信息内容
        mBuilder.setTitle("普通对话框");
        mBuilder.setIcon(R.drawable.icon8);
        mBuilder.setMessage("请输入账号和密码");

        //调用对话框对象的setPositiveButton方法为AlertDialog对话框内部设置按钮
        mBuilder.setPositiveButton("确定", new okClick());
        mBuilder.create();

        btn_dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name1 = edt_Name.getText().toString();
                String psd = edt_Password.getText().toString();
                if (name1.equals("") || psd.length() == 0) {
                    mBuilder.show();
                } else if (psd.length() != 6) {
                    Toast.makeText(getApplicationContext(), "密码输入错误！请重新输入！", Toast.LENGTH_SHORT).show();
                } else {
                    intent = new Intent(MainActivity.this, FirstActivity.class);
                    String name2 = edt_Name.getText().toString();
                    intent.putExtra("用户", name2);
                    startActivity(intent);
                }


            }
        });

        btn_zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
        //使用沉浸式模式，隐藏导航栏
        /*public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus && Build.VERSION.SDK_INT >= 19) {
                View decorView = getWindow().getDecorView();
                decorView.setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            }
        }*/


    }

    //创建OnClickListener接口的内部类并重写OnClick方法
    class okClick implements DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
        }
    }
}
